/*
This -do file generates tables of regression analysis, graphs to be used in the 
analysis of the Malawi student incentive project
 
Last version: Hyuk Son, 10/04/2016
 
uses:
	merged_y1y2.dta

saves:	
		attrition.tex
		balance.tex
		s_expectation.tex
		s_exam.tex
		s_input.tex
		s_noncog.tex
		s_teacher.tex
		f_exam.tex
		f_input.tex
		f_noncog.tex
		f_teacher.tex
		
		
Contents
 
*             0 Set Up
*                   0.1 Assign groups of control variables
*					0.2 Sample restriction
*
*             1 Main Tables	
*					1.1 Sample composition by Treatment category
*					1.2 Baseline balance check 
*					1.3 Scholarship effect: Understanding and expectation
*					1.4 Scholarship effect: Test score impacts
*					1.5 Scholarship effect: Intermediate outcomes
*					1.6 Scholarship effect: Long term test score impacts
*					1.7 Scholarship effect: Long term Intermediate outcomes
*					1.8 Feedback effect: Test score impacts
*					1.9 Appendix 1: Baseline balance check using long-term follow-up sample
*					1.10 Appendix 2: Sample attrition
*					1.11 Appendix 3: Understanding and Expectation within subgroup distribution
*					1.12 Appendix 4: Test Score Impacts within subgroup distribution
*					1.13 Appendix 5: Test score impacts (Noncognitive skills controlled)
* 					1.14 Appendix 6: Classroom environment
*					1.15 Appendix 7: Impact on scores with no incentive
*					1.16 Appendix 8: Feedback effect: Feedback intensity
*					1.17 Appendix 9: Control group contamination
*
*             2 Table for presentation	
*					2.1 Baseline balance check
*					2.2 Sample attrition
*					2.3 Scholarship effect: Understanding and expectation
*					2.4 Scholarship effect: Exam results and cognitive traits
*					2.5 Scholarship effect: Student investment
*					2.6 Scholarship effect: Motivation and non-cognitive traits
*					2.7 Scholarship effect: Teachers and family response 
*					2.8 Feedback effect: Exam results and cognitive traits
*					2.9 Feedback effect: Student investment
*					2.10 Feedback effect: Motivation and non-cognitive traits
*					2.11 Feedback effect: Teachers and family response 
*/

/*********************************************************************************************************************

  					0	Set up

*********************************************************************************************************************/
set more off
clear all

include "$do_path/programs.do"

****************************************************
/* 		0.3 Read in data		 */
**************************************************** 
use "$data_path/merged_y1y2.dta", clear

*** variable label 
	lab var b1_s9_conscientious "Conscientiousness"
	lab var b1_teachereffort_index "Teacher Index"
	lab var male "Male"
	lab var merit "\textit{Standard}"
	lab var relative "\textit{Relative}"
	lab var b1_top15 "Top 15\%"
	lab var m_b1top15 "\textit{Std.} x Top 15\%"
	lab var r_b1top15 "\textit{Rel.} x Top 15\%"	
	lab var b1_sub15 "Subg. Top 15\%"
	lab var m_b1sub15 "\textit{Std.} x Subg. Top 15\%"
	lab var r_b1sub15 "\textit{Rel.} x Subg. Top 15\%"	
	lab var b1_studyhour "Study hours per week"
	lab var b1_pct "Baseline rank(\%)"
	lab var b1_z_total "Baseline Score"
	lab var b1_hhsize "Household size"
	lab var meritfeed "\textit{Std.} x FB"
	lab var relativefeed "\textit{Rel.} x FB"
	lab var b1_parentaleffort_index "Parental Effort Index"


****************************************************
/* 		0.4 Assign groups of outcome and control variables		 */
****************************************************

 * 0.1.1 Assign outcome variables
	local t2a age male b1_q105_chewa b1_hhsize b1_s5_asset
	local t3 understand_scholarship f1_understand_scholarship expect_scholarship2 f1_expect_scholarship2  
	local t4 f1_pct f1_z_total f1_s9_score  
	local t5 atd_abs_aa0m f1_studyhour_ac y1_jun15_abs y1_may15_abs y1_apr15_abs y1_mar15_abs
	local t6 f1_motivation f1_s7_selfesteem f1_s7_conscientious f1_s7_grit
	local t7 f1_teachereffort_index f1_parentaleffort_index f1_q503 f1_parentalinvolve

 * 0.1.2 Assign control variables
	local demo i.age male b1_q105_chewa b1_hhsize b1_s5_asset	
	local demo1 *.age male b1_q105_chewa b1_hhsize b1_s5_asset	
	local cons4 b1_pct b1_z_total 
	local cons5 atd_abs_bam b1_studyhour 
	local cons6 b1_motivation b1_s9_selfesteem b1_s9_conscientious b1_s9_grit 
	local cons7 b1_teachereffort_index b1_parentaleffort_index

	local base age male b1_q105_chewa b1_hhsize b1_s5_asset b1_pct b1_z_total atd_abs_bam b1_studyhour b1_motivation b1_s9_selfesteem b1_s9_grit b1_s9_conscientious b1_teacherindex b1_parentaleffort_index
		
	global cons `cons'

****************************************************
/* 		0.5 Set up specifications		 */
****************************************************
** Scholarship effect
	loc s_spec merit relative `demo' i.y1_zone i.y1_standard // Scholarship effect, Base specification 
	loc s_spec_top merit relative m_b1top15 r_b1top15 b1_top15 `demo' i.y1_zone i.y1_standard // Scholarship effect, Heterogeneity across overall distribution
	loc s_spec_sub merit relative m_b1sub15 r_b1sub15 b1_sub15 `demo' i.y1_zone i.y1_standard // Scholarship effect, Heterogeneity within subgroup
	loc s_spec_nocon merit relative i.y1_standard // Scholarship effect, Base specification 
	loc s_spec_top_nocon merit relative m_b1top15 r_b1top15 b1_top15 i.y1_standard // Scholarship effect, Heterogeneity across overall distribution
	loc s_spec_sub_nocon merit relative m_b1sub15 r_b1sub15 b1_sub15 i.y1_standard // Scholarship effect, Heterogeneity within subgroup

** Feeback effect
	loc f_spec feedback `demo' i.y1_zone i.y1_standard
	loc f_spec_hetero feedback merit relative meritfeed relativefeed `demo' i.y1_zone i.y1_standard

** Standard error
	loc secluster r cluster(y1_school_class)

****************************************************
/* 		0.6 Table set up		 */
******************************************************  
	loc regtable cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) label varlabels(, end("" [1ex]) nolast) mlabels(none) collabels(none) nonotes addnotes() 

********************************************************************************************************************
/*
  					1 Main Tables
*/
********************************************************************************************************************
****************************************************
/* 		1.1  Sample Composition				 */
****************************************************
*** Compute numbers
 ** N of students
 preserve
 keep if y1_sub==1

	forval i = 1/3 {
		qui sum y1_standard if y1_rand==`i'  // N of students for each scholrarhip group 
		loc st`i'=r(N)
		
		qui sum y1_standard if y1_rand==`i' & y1_standard==5 | y1_rand==`i' & y1_standard==6 // N of students for each scholrarhip group (in long-term f/u sample)
		loc l_st`i'=r(N)
	}

	qui sum y1_standard // Total N of students 
	loc st=r(N)

	qui sum y1_standard  if y1_standard==5 | y1_standard==6 // N of students in long-term f/u sample
	loc l_st=r(N)

 ** N of classrooms
collapse (mean) y1_rand y1_standard, by(y1_school_class)
	forval i = 1/3 {
		qui sum y1_school_class if y1_rand==`i'
		loc cl`i'=r(N)

		qui sum y1_school_class if y1_rand==`i' & y1_standard==5 | y1_rand==`i' & y1_standard==6
		loc l_cl`i'=r(N)
	}

	qui sum y1_school_class 
	loc cl=r(N)

	qui sum y1_school_class if  y1_standard==5 | y1_standard==6 
	loc l_cl=r(N)

restore

*** Writing table
file open sampcomp using "$tex_path/sample_comp1.tex", write replace

file write sampcomp ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
		"\caption{Sample Composition by Treatment Category\label{tab:sample}}" _n ///
		"\begin{tabular}{lcc}" _n ///
			"\tabularnewline [0.02in] \multicolumn{2}{l}{ Panel A: Scholarship Treatment (Grade 5-8)} \tabularnewline" _n ///
			"\hline " _n ///
			"Scholarship Assignment &  School-Grades & Students \tabularnewline" _n ///
			"\hline " _n ///
			"{\textit{Standard} scholarship} & `cl1' & `st1' \tabularnewline" _n ///
			"{\textit{Relative} scholarship} &`cl2' & `st2' \tabularnewline" _n ///
			"Control & `cl3' & `st3' \tabularnewline " _n ///
			"\hline " _n ///
			"Total  & `cl' & `st' \tabularnewline" _n ///
			"\hline " _n ///
			"\tabularnewline [0.02in] \multicolumn{2}{l}{Panel B: Scholarship Treatment (Grade 5-6 with longer-term follow-up)} \tabularnewline" _n ///
			"\hline " _n ///
			"Scholarship Assignment &  School-Grades & Students \tabularnewline" _n ///
			"\hline " _n ///
			"{\textit{Standard} scholarship} & `l_cl1' & `l_st1' \tabularnewline" _n ///
			"{\textit{Relative} scholarship} & `l_cl2' & `l_st2' \tabularnewline" _n ///
			"Control & `l_cl3' & `l_st3' \tabularnewline " _n ///
			"\hline " _n ///
			"Total  & `l_cl' & `l_st' \tabularnewline" _n ///
			"\hline" _n ///
		"\end{tabular}" _n ///
	"\begin{tablenotes}[flushleft]" _n ///
		"\item" _n ///
		"Notes: The scholarship assignment was randomized at the school-grade level with stratification by grade. " ///
	"\end{tablenotes}" _n ///
	"\end{threeparttable}" _n ///
"\end{table}"

file close sampcomp


****************************************************
/* 		1.2 	Baseline balance check				 */
****************************************************
preserve
keep if y1_sub==1
local rows: word count `t2a' `cons4' `cons5' `cons6' `cons7'

estimates clear

eststo: estpost summarize `t2a' `cons4' `cons5' `cons6' `cons7' if control==1
qui esttab *, cells(mean)
mat B2=r(coefs)
qui esttab *, cells(sd)
mat SE2=r(coefs)
mat P2=J(`rows',1,.)

estimates clear

foreach var of varlist `t2a' `cons4' `cons5' `cons6' `cons7' {
	reg `var' merit relative i.y1_standard, cluster(y1_school_class)
	estimates store `var'
	}

esttab *, cells(b) nocons keep(merit relative)
mat B3=r(coefs)',r(stats)'
esttab *, cells(se) nocons
mat SE3=r(coefs)',J(`rows',1,.)
esttab *, cells(p) nocons
mat P3=r(coefs)',J(`rows',1,.)

estimates clear

mat B=B2,B3
mat SE=SE2,SE3
mat P=P2,P3

file open balance using "$tex_path/balance.tex", write replace

file write balance ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{footnotesize}" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\caption{Balance of Baseline Variables Across Treatment Groups\label{tab:balance}}" _n ///
	"\begin{tabular}{l*{4}{c}}" _n ///
	"\hline" _n ///
	" & Control & \textit{Standard} vs. & \textit{Relative} vs. &  \\" _n  ///
	" & Mean & Control & Control & N  \\" _n ///
	"\cmidrule(lr){2-5}" _n ///
	"&(1)&(2)&(3)&(4)\\" _n ///
	"\hline" _n
	
	
local i 1
foreach var in `t2a' `cons4' `cons5' `cons6' `cons7' {
	local label: var label `var'
	foreach j of num 1/1 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		local se`j'="[`se`j'']"
		}
	foreach j of num 2/4 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local pvalue=P[`i',`j']
		if `pvalue'<0.01 local star ***
		else if `pvalue'<0.05 local star **
		else if `pvalue'<.1 local star *
		else local star
		if "`star'"!="" local sym "\sym{`star'}"
		else local sym
		local b`j' `b`j''`sym'
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		if `se`j''!=. local se`j'="(`se`j'')"
		else local se`j' 
		}
	file write balance ///
	"`label'&`b1'&`b2'&`b3'&`b4' \\" _n ///
	" &`se1'&`se2'&`se3'&`se4' \\" _n 
	if `i'<`rows' file write balance "[1ex]"_n 
	local ++i
	}
	
file write balance ///
	"\hline" _n ///
	"\end{tabular}" _n ///
	"\begin{tablenotes}[flushleft]" _n ///
	"\item Notes: Column 1 reports means for subjects assigned to the control group. " ///
	" Columns 2 and 3 report mean differences between the scholarship treatment groups and the control group." ///
	"Standard deviations are in brackets, and standard errors, clustered at the school-grade level, are in parentheses. " ///
	"Test scores are normalized using the control group mean and standard deviation. " ///
	"The asset index is constructed as the 1st principal component of variables indicating the ownership of 26 assets. " ///
	"Teacher and parental effort indices are aggregates of the seven and four measures, respectively. " ///
	"Indices are generated by taking the average of the standardized measures where the mean and standard deviation in the control group is used in the standardization. " ///
	"The resulting index is then standardized relative to the control group. " ///
	"Self-esteem, grit, and conscientiousness measures are simple averages of questions measured on a four-point scale (self-esteem) or five-point scale (grit and conscientiousness). " ///
	"* denotes significance at 0.10; ** at 0.05; and *** at 0.01." _n ///
	"\end{tablenotes}" _n ///
	"\end{threeparttable}" _n ///
	"\end{footnotesize}" _n ///
	"\end{table}" 

file close balance




mat B=B2,B3
mat SE=SE2,SE3
mat P=P2,P3
	
file open balance using "$present_path/balance_present_1.tex", write replace

file write balance ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{scriptsize}" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\begin{tabular}{l*{4}{c}}" _n ///
	"\hline" _n ///
	" & Control & \textit{Standard} &  \textit{Relative} &  \\" _n  ///
	"& Mean & vs.Control & vs.Control & N \\" _n ///
	"\cmidrule(r){2-5}" _n ///
	"&(1)&(2)&(3)&(4) \\" _n ///
	"\hline" _n
	
	
local i 1
foreach var in `t2a' `cons4' {
	local label: var label `var'
	foreach j of num 1/1 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		local se`j'="[`se`j'']"
		}
	foreach j of num 2/4 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local pvalue=P[`i',`j']
		if `pvalue'<0.01 local star ***
		else if `pvalue'<0.05 local star **
		else if `pvalue'<.1 local star *
		else local star
		if "`star'"!="" local sym "\sym{`star'}"
		else local sym
		local b`j' `b`j''`sym'
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		if `se`j''!=. local se`j'="(`se`j'')"
		else local se`j' 
		}
	file write balance ///
	"`label'&`b1'&`b2'&`b3'&`b4' \\" _n ///
	" &`se1'&`se2'&`se3'&`se4' \\" _n 
	if `i'<`rows' file write balance "[.5ex]"_n 
	local ++i
	}
	
file write balance ///
	"\hline" _n ///
	"\end{tabular}" _n ///
	"\end{threeparttable}" _n ///
	"\end{scriptsize}" _n ///
	"\end{table}" 

file close balance

file open balance using "$present_path/balance_present_2.tex", write replace

file write balance ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{scriptsize}" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\begin{tabular}{l*{4}{c}}" _n ///
	"\hline" _n ///
	" & Control & \textit{Standard}  &  \textit{Relative} & \\" _n  ///
	"& Mean & vs.Control & vs.Control & N \\" _n ///
	"\cmidrule(r){2-5}" _n ///
	"&(1)&(2)&(3)&(4) \\" _n ///
	"\hline" _n
	
foreach var in `cons5' `cons6' `cons7' {
	local label: var label `var'
	foreach j of num 1/1 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		local se`j'="[`se`j'']"
		}
	foreach j of num 2/4 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local pvalue=P[`i',`j']
		if `pvalue'<0.01 local star ***
		else if `pvalue'<0.05 local star **
		else if `pvalue'<.1 local star *
		else local star
		if "`star'"!="" local sym "\sym{`star'}"
		else local sym
		local b`j' `b`j''`sym'
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		if `se`j''!=. local se`j'="(`se`j'')"
		else local se`j' 
		}
	file write balance ///
	"`label'&`b1'&`b2'&`b3'&`b4' \\" _n ///
	" &`se1'&`se2'&`se3'&`se4' \\" _n 
	if `i'<`rows' file write balance "[.5ex]"_n 
	local ++i
	}
	
file write balance ///
	"\hline" _n ///
	"\end{tabular}" _n ///
	"\end{threeparttable}" _n ///
	"\end{scriptsize}" _n ///
	"\end{table}" 

file close balance
est clear
restore

********************************************************************************************************
/* 		1.3 	Scholarship effect: Understanding and Expectation				 */
********************************************************************************************************
** Grade 5-8
preserve
keep if y1_sub==1
	forval i = 1/4 {
	local v: word `i' of `t3'
	quietly eststo: reg `v' `s_spec', `secluster'
	quietly estadd ysumm
	quietly test merit=relative
	quietly estadd scalar p_diff=r(p)

	quietly eststo: reg `v' `s_spec_top', `secluster'
	quietly estadd ysumm

	quietly eststo: reg `v' `s_spec_sub' `j', `secluster'
	quietly estadd ysumm	
	} 
restore

local caption "Understanding and Expectation"
local label expectation
local notes \item Notes: In Columns 1 and 2, the dependent variable reflects the percent of questions answered correctly ///
		on the test of understanding of the scholarship schemes. ///
		In Columns 3 and 4, the dependent variable is an indicator that equals 1 ///
		if the student answered [very likely] or [likely] to the question: ///
		[Based on your current position, how much do you think you have a chance of receiving the gift?] ///
		Standard errors, clustered at the classroom level, are in parentheses. ///
		All specifications include grade fixed effects, district fixed effects, ///
		age, ethnic group, household size, and a household asset ///
		index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.

 ** paper
head_mid, caption(`caption') label(`label') notes(`notes') columns(4) size(footnotesize)

	esttab est1 est4 est7 est10 using "$tex_path/s_expectation.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" After Announcement" "  1st Follow-up" " After Announcement" "  1st Follow-up" " After Announcement" , pattern(1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.8in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{4}{c}{Sample: Grade 5-8}\tabularnewline\cmidrule(r){2-5}\rule{0pt}{3ex}\rule{0pt}{2ex}&\multicolumn{2}{c}{Understanding}&\multicolumn{2}{c}{Expectation} \\ \cmidrule(r){2-3}\cmidrule(r){4-5}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")

mid_foot, size(footnotesize)

	esttab est2 est5 est8 est11 using "$tex_path/s_expectation.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(r2, fmt(3) labels("R-Squared"))  ///
	prehead("$header2") posthead("$mid2") postfoot("\hline") 

mid_foot, notes(`notes') size(footnotesize)

	esttab est3 est6 est9 est12 using "$tex_path/s_expectation.tex", ///
	append keep(merit relative m_b1sub15 r_b1sub15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	indicate("Controls=`demo1'") ///	
	prehead("$header2") posthead("$mid3")  postfoot("$footer2") 


*** presentation
head_foot, label(`label') size(footnotesize) columns(4)

esttab est1 est2 est4 est5 using "$present_path//s_understanding_present.tex", ///
	replace keep(merit relative b1_top15 m_b1top15 r_b1top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups("After Announcement" "Follow-up" , pattern(1 0 1 0 ) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span})) ///
	prehead("$header") postfoot("$footer")

esttab est7 est8 est10 est11 using "$present_path/s_expectation_present.tex", ///
	replace keep(merit relative b1_top15 m_b1top15 r_b1top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups("After Announcement" "Follow-up" , pattern(1 0 1 0 ) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span})) ///
	prehead("$header") postfoot("$footer")

estimates clear	

********************************************************************************************************
/* 		1.4 	Scholarship effect: Test score impacts	(Short and Long-term)			 */
********************************************************************************************************
** Grade 5-8
preserve
keep if y1_sub==1
	forval k=1/2 {
	local i : word `k' of f1_pct f1_z_total
    local j : word `k' of b1_pct b1_z_total
 	 eststo: reg `i' `s_spec_nocon' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)

	 eststo: reg `i' `s_spec_top_nocon' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff2_1=r(p)
	 quietly test merit+m_b1top15=relative+r_b1top15
	 quietly estadd scalar p_diff2=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff2_1=r(p)
	 quietly test merit+m_b1top15=relative+r_b1top15
	 quietly estadd scalar p_diff2=r(p)

	 eststo: reg `i' `s_spec_sub_nocon' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff3_1=r(p)
	 quietly test merit+m_b1sub15=relative+r_b1sub15
	 quietly estadd scalar p_diff3=r(p)
	 eststo: reg `i' `s_spec_sub' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff3_1=r(p)
	 quietly test merit+m_b1sub15=relative+r_b1sub15
	 quietly estadd scalar p_diff3=r(p)
	}
restore

** Grade 5-6
preserve
keep if y1_sub==1
keep if y1_standard==5|y1_standard==6	
	forval k=1/3 {
	local i : word `k' of f1_z_total stu_tot_z_16_1 stu_tot_z_16_2
    local j : word `k' of b1_z_total b1_z_total b1_z_total
	 eststo: reg `i' `s_spec_nocon', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)

	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)

	 eststo: reg `i' `s_spec_top_nocon' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm

	 eststo: reg `i' `s_spec_sub_nocon' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster'
	 estadd ysumm
	}
restore

local caption "Test Score Impacts"
local label "s-exam"
local notes \item Notes: Standard errors, clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects and the baseline value of the outcome variable. ///
 	Additional controls include zone fixed effects, age, ethnic group, household size, and ///
 	a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.
	
*** paper
 * short term result
head_mid, caption(`caption') label(`label') columns(4) size(footnotesize)

	esttab est1 est2 est7 est8 using "$tex_path/s_exam.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Exam Rank" " Exam score"  , pattern(1 0 1 0 ) /// 
		span prefix(\multicolumn{@span}{p{1.2in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{4}{c}{Sample: Grade 5-8}\tabularnewline \cmidrule(r){2-5}\rule{0pt}{2ex}&\multicolumn{4}{c}{1st Follow-up}\tabularnewline \cmidrule(r){2-5}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")

mid_foot, size(footnotesize)

	esttab est3 est4 est9 est10 using "$tex_path/s_exam.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(r2 p_diff2_1 p_diff2, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel} at Bot. 85\%" "P-value: \textit{Std = Rel} at Top 15\%"))  ///
	prehead("$header2") posthead("$mid2") postfoot("\hline") 

mid_foot, notes(`notes') size(footnotesize)

	esttab est5 est6 est11 est12 using "$tex_path/s_exam.tex", ///
	append keep(merit relative m_b1sub15 r_b1sub15) `regtable' nonum ///
	stats(N r2 ymean p_diff3_1 p_diff3, fmt(0 3 3 3) labels("N" "R-Squared" "Mean of Dep. Var." "P-value: \textit{Std = Rel} at Bot. 85\%" "P-value: \textit{Std = Rel} at Top 15\%"))  ///
	indicate("Additional controls=`demo1'") ///	
	prehead("$header2") posthead("$mid3")  postfoot("$footer2") 

 * long term result
 local caption "Longer Term Test Score Impacts"
 local label "s-exam-l"

head_mid, caption(`caption') label(`label') columns(4) size(small)

	esttab est13 est14 est19 est20  using "$tex_path/s_exam_long.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" 1st Follow-up" " 2nd Follow-up", pattern(1 0 1 0) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{4}{c}{Sample: Grade 5-6}\tabularnewline \cmidrule(r){2-5}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")


mid_foot, notes(`notes') size(small)

	esttab est15 est16 est21 est22  using "$tex_path/s_exam_long.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	indicate("Controls=`demo1'") ///	
	prehead("$header2") posthead("$mid2")  postfoot("$footer2") 

*** presentation (short-term results)
head_foot, label(`label') size(footnotesize) columns(4)

esttab est2 est4 est8 est10 using "$present_path/s_exam_present_short.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Exam Rank" " Exam Score", pattern(1 0 1 0) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{1ex}&\multicolumn{4}{c}{Grade 5-8} \tabularnewline \cmidrule(r){2-5} \rule{0pt}{2ex}")) ///	
	prehead("$header") postfoot("$footer")

head_foot, label(`label') size(footnotesize) columns(4)

esttab est6 est12 using "$present_path/s_exam_present_short_sub.tex", ///
	replace keep(merit relative m_b1sub15 r_b1sub15 b1_sub15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Exam Rank" " Exam Score", pattern(1 1) /// 
		prefix(\multicolumn{@span}{p{0.8in}}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{1ex}&\multicolumn{2}{c}{Grade 5-8} \tabularnewline \cmidrule(r){2-3} \rule{0pt}{2ex}")) ///	
	prehead("$header") postfoot("$footer")


*** presentation (longer-term restuls)
head_foot, label(`label') size(footnotesize) columns(4)

esttab est14 est16 est20 est22 using "$present_path/s_exam_long_present.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" 1st f/u" " 2nd f/u", pattern(1 0 1 0) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{1ex}&\multicolumn{4}{c}{Grade 5-6} \tabularnewline \cmidrule(r){2-5} \rule{0pt}{2ex}")) ///	
	prehead("$header") postfoot("$footer")
est clear


****************************************************
/* 		1.4.2 Heterogeneity by baseline score (in detail)  */
****************************************************
 ** variable generation
* Quintile
	gen b1q1 = inrange(b1_pct,0,20)
	gen b1q2 = inrange(b1_pct,20,40)
	replace b1q2=0 if b1_pct==20
	gen b1q3 = inrange(b1_pct,40,60)
	replace b1q3=0 if b1_pct==40
	gen b1q4 = inrange(b1_pct,60,80)
	replace b1q4=0 if b1_pct==60
	gen b1q5 = inrange(b1_pct,80,100)
	replace b1q5=0 if b1_pct==80
*/
	gen b1sq1 = inrange(b1_subpct,0,20)
	gen b1sq2 = inrange(b1_subpct,20,40)
	replace b1sq2=0 if b1_subpct==20
	gen b1sq3 = inrange(b1_subpct,40,60)
	replace b1sq3=0 if b1_subpct==40
	gen b1sq4 = inrange(b1_subpct,60,80)
	replace b1sq4=0 if b1_subpct==60
	gen b1sq5 = inrange(b1_subpct,80,100)
	replace b1sq5=0 if b1_subpct==80
*/

forval i = 1/5 {
gen m_b1q`i' = merit * b1q`i'
gen r_b1q`i' = relative * b1q`i'

gen m_b1sq`i' = merit * b1sq`i'
gen r_b1sq`i' = relative * b1sq`i'
}

** labeling variables
	forval i = 1/5 {
		loc j: word `i' of 5th 4th 3rd 2nd 1st
		lab var b1q`i' "Baseline `j' quintile"
		lab var m_b1q`i' "\textit{Standard} $ \times$ Baseline `j' quintile"
		lab var r_b1q`i' "\textit{Relative} $ \times$ Baseline `j' quintile"

		lab var b1sq`i' "Baseline `j' quintile"
		lab var m_b1sq`i' "\textit{Standard} $ \times$ Baseline `j' quintile in bin"
		lab var r_b1sq`i' "\textit{Relative} $ \times$ Baseline `j' quintile in bin"
}



*** Estimation 
	eststo: reg f1_z_total m_b1q1 m_b1q2 m_b1q3 m_b1q4 m_b1q5 ///
				r_b1q1 r_b1q2 r_b1q3 r_b1q4 r_b1q5 ///
				b1q1 b1q2 b1q3 b1q4 b1q5 ///
 				b1_z_total `demo' i.y1_zone i.y1_standard, `secluster' noconstant
				
				
 	forval i = 1/5 {
		lincom m_b1q`i'
		loc m`i'_b = r(estimate)
		loc m`i'_se = r(se)
		loc m`i'_p = 2*ttail(r(df), abs(r(estimate)/r(se)))

		lincom r_b1q`i'
		loc r`i'_b = r(estimate)
		loc r`i'_se = r(se)
		loc r`i'_p = 2*ttail(r(df), abs(r(estimate)/r(se)))

		lincom m_b1q`i'-r_b1q`i'
		loc mr`i'_b = r(estimate)
		loc mr`i'_se = r(se)
		loc mr`i'_p = 2*ttail(r(df), abs(r(estimate)/r(se)))
  	}

	foreach j of num 1/15 {
		loc est_b: word `j' of m1_b m2_b m3_b m4_b m5_b r1_b r2_b r3_b r4_b r5_b mr1_b mr2_b mr3_b mr4_b mr5_b 
		loc est_se: word `j' of m1_se m2_se m3_se m4_se m5_se r1_se r2_se r3_se r4_se r5_se mr1_se mr2_se mr3_se mr4_se mr5_se 
		loc est_p: word `j' of m1_p m2_p m3_p m4_p m5_p r1_p r2_p r3_p r4_p r5_p mr1_p mr2_p mr3_p mr4_p mr5_p 

		autoformat ``est_b''
		local est_b`j': di $format ``est_b''
		local pvalue ``est_p''
		if `pvalue'<0.01 local star ***
		else if `pvalue'<0.05 local star **
		else if `pvalue'<.1 local star *
		else local star
		if "`star'"!="" local sym "\sym{`star'}"
		else local sym
		local est_b`j' `est_b`j''`sym'

		autoformat ``est_se''
		local est_se: di $format ``est_se''
		local est_se=strtrim("`est_se'")
		if `est_se'!=. local est_se`j'="(`est_se')"
		else local est_se`j'
		}


*** Table

file open distribution using "$tex_path/s_exam_dist.tex", write replace

file write distribution ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{footnotesize}" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\caption{Heterogeneity by Quintile of Baseline Test Score\label{tab:s-exam-distribution}}" _n ///
	"\begin{tabular}{l*{3}{c}}" _n ///
	"\hline" _n ///
	"& \multicolumn{3}{c}{Sample Grade: 5-8} \\" _n ///
	"\cmidrule(r){2-4}" _n ///
	"& \multicolumn{3}{c}{Exam Score} \\" _n ///
	"\cmidrule(r){2-4}" _n ///
	" & \multicolumn{1}{p{0.8in}}{\centering \textit{Standard}-Control } & \multicolumn{1}{p{0.8in}}{\centering \textit{Relative}-Control} & \multicolumn{1}{p{0.8in}}{\centering \textit{Standard}-\textit{Relative}}  \\" _n ///
	"& \multicolumn{1}{c}{\centering $ \theta_k^s $} & \multicolumn{1}{c}{\centering $ \theta_k^r $} & \multicolumn{1}{c}{\centering $ \theta_k^s-\theta_k^r $}  \\" _n ///
	"\cmidrule(lr){2-2}\cmidrule(l){3-3}\cmidrule(l){4-4}" _n ///
	"&(1)&(2)&(3) \\" _n ///
	"\hline" _n ///
	" Fifth Quintile (Lowest) &`est_b1'&`est_b6'&`est_b11' \\" _n ///
	" 				  &`est_se1'&`est_se6'&`est_se11' \\" _n ///
	" Fourth Quintile &`est_b2'&`est_b7'&`est_b12' \\" _n ///
	" 				  &`est_se2'&`est_se7'&`est_se12' \\" _n ///
	" Third Quintile &`est_b3'&`est_b8'&`est_b13' \\" _n ///
	" 				  &`est_se3'&`est_se8'&`est_se13' \\" _n ///
	" Second Quintile &`est_b4'&`est_b9'&`est_b14' \\" _n ///
	" 				  &`est_se4'&`est_se9'&`est_se14' \\" _n ///
	" First Quintile (Highest) &`est_b5'&`est_b10'&`est_b15' \\" _n ///
	" 				  &`est_se5'&`est_se10'&`est_se15' \\" _n ///
	"\hline" _n ///
	"\end{tabular}" _n ///
	"\begin{tablenotes}[flushleft]" _n ///
	"\item Notes: Standard errors, clustered at the school-grade level, are in parentheses. " ///
 	"Controls include baseline test score, grade and zone fixed effects, age, ethnic group, household size, " ///
	"and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01." ///
	"\end{tablenotes}" _n ///
	"\end{threeparttable}" _n ///
	"\end{footnotesize}" _n ///
	"\end{table}" 

file close distribution

file open distribution using "$present_path/s_exam_dist_present.tex", write replace

file write distribution ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{footnotesize}" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\resizebox{0.8\textwidth}{!}{\begin{tabular}{l*{3}{c}}" _n ///
	"\hline" _n ///
	"& \multicolumn{3}{c}{Grade 5-8, Exam Score} \\" _n ///
	"\cmidrule(r){2-4}" _n ///
	" & \multicolumn{1}{p{0.8in}}{\centering \textit{Standard}-Control} & \multicolumn{1}{p{0.8in}}{\centering \textit{Relative}-Control} & \multicolumn{1}{p{0.8in}}{\centering \textit{Standard}-\textit{Relative}}  \\" _n ///
	"& \multicolumn{1}{c}{\centering $ \theta_k^s $} & \multicolumn{1}{c}{\centering $ \theta_k^r $} & \multicolumn{1}{c}{\centering $ \theta_k^s-\theta_k^r $}  \\" _n ///
	"\cmidrule(lr){2-2}\cmidrule(l){3-3}\cmidrule(l){4-4}" _n ///
	"&(1)&(2)&(3) \\" _n ///
	"\hline" _n ///
	" Fifth Quintile (Lowest) &`est_b1'&`est_b6'&`est_b11' \\" _n ///
	" 				  &`est_se1'&`est_se6'&`est_se11' \\" _n ///
	" Fourth Quintile &`est_b2'&`est_b7'&`est_b12' \\" _n ///
	" 				  &`est_se2'&`est_se7'&`est_se12' \\" _n ///
	" Third Quintile &`est_b3'&`est_b8'&`est_b13' \\" _n ///
	" 				  &`est_se3'&`est_se8'&`est_se13' \\" _n ///
	" Second Quintile &`est_b4'&`est_b9'&`est_b14' \\" _n ///
	" 				  &`est_se4'&`est_se9'&`est_se14' \\" _n ///
	" First Quintile (Highest) &`est_b5'&`est_b10'&`est_b15' \\" _n ///
	" 				  &`est_se5'&`est_se10'&`est_se15' \\" _n ///
	"\hline" _n ///
	"\end{tabular}}" _n ///
	"\end{threeparttable}" _n ///
	"\end{footnotesize}" _n ///
	"\end{table}" 

file close distribution

est clear

***************************************************
/* 		1.5 	Scholarship effect: Intermediate outcomes	(Short and Long-term)		 */
****************************************************
** Generate non-cognitive skill index 
	genindex f1_motivation f1_s7_selfesteem f1_s7_grit f1_s7_conscientious, prefix(f1_noncog) label("Follow-up Non-cognitive Skill Index")
	genindex b1_motivation b1_s9_selfesteem b1_s9_grit b1_s9_conscientious, prefix(b1_noncog) label("Baseline Non-cognitive Skill Index")
	
	egen f1_noncog_idx= rowtotal(f1_motivation f1_s7_selfesteem f1_s7_grit f1_s7_conscientious) if f1_motivation~=.|f1_s7_selfesteem~=.|f1_s7_grit~=.| f1_s7_conscientious~=.
	egen b1_noncog_idx= rowtotal(b1_motivation b1_s9_selfesteem b1_s9_grit b1_s9_conscientious) if b1_motivation~=.| b1_s9_selfesteem~=.| b1_s9_grit~=.| b1_s9_conscientious~=.

** varaible grouping
	local score f1_q213g m1_z_total
	local t5i atd_abs_aa0m f1_studyhour_ac
	local t6i f1_motivation f1_s7_selfesteem f1_s7_grit f1_s7_conscientious f1_noncog_index
	local t7i f1_teachereffort_index f1_parentaleffort_index f1_q503 
	local scorecon b1_q216g b1_z_total
	local cons5i atd_abs_bam b1_studyhour 
	local cons6i b1_motivation b1_s9_selfesteem b1_s9_grit b1_s9_conscientious b1_noncog_index
	local cons7i b1_teacherindex b1_parentaleffort_index

** Short term result
preserve
keep if y1_sub==1
	forval k=1/10 {
	local i : word `k' of `t5i' `t6i' `t7i'
    local j : word `k' of `cons5i' `cons6i' `cons7i'
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)

	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	}
restore

local caption "Intermediate Outcomes"
local label "s-intermediate"
local notes \item Notes: Standard errors, clustered at the school-grade level, are in parentheses. ///
 	Controls include baseline test score, grade and zone fixed effects, age, ethnic group, household size, ///
 	and a household asset index. Teacher and parental effort indices are aggregates of the ///
 	seven and four measures, respectively. Indices are generated by taking the average of ///
 	the standardized measures where the mean and standard deviation in the control group is used in the standardization. ///
 	The resulting index is then standardized relative to the ///
 	control group. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 
	
*** paper
head_mid, caption(`caption') label(`label') columns(10) size(scriptsize) sideways

esttab est1 est3 est5 est7 est9 est11 est13 est15 est17 est19 using "$tex_path/s_intermediate.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Attendance" " Study Hours" " Motivation to study hard" " Self esteem" " Grit" " Conscien tiousness" " Non-cognitive skill index" " Teacher effort index" " Parental effort" " Parents mentioned scholarship", pattern(1 1 1 1 1 1 1 1 1 1) /// 
 	span prefix(\multicolumn{@span}{p{0.54in}}{\centering) suffix(}) ///
			erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex} &\multicolumn{9}{c}{Sample: Grade 5-8}&\tabularnewline \cmidrule(r){2-11} \rule{0pt}{2ex}&\multicolumn{2}{c}{Student input}&\multicolumn{5}{c}{Non-cognitive skills}&\multicolumn{3}{c}{Teacher and parental response}\tabularnewline \cmidrule(r){2-3}\cmidrule(r){4-8}\cmidrule(r){9-11}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")

mid_foot, notes(`notes') size(scriptsize) sideways

esttab est2 est4 est6 est8 est10 est12 est14 est16 est18 est20 using "$tex_path/s_intermediate.tex", ///
	append keep(merit relative m_b1top15 r_b1top15  b1_top15) `regtable' nonum  ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	prehead("$header2") posthead("$mid2")  postfoot("$footer2") 
est clear


** Short term result
preserve
keep if y1_sub==1
	forval k=1/10 {
	local i : word `k' of `t5i' `t6i' `t7i'
    local j : word `k' of `cons5i' `cons6i' `cons7i'
	 eststo: reg `i' `s_spec_sub' `j', `secluster'
	 estadd ysumm
	}
restore

local caption "Intermediate Outcomes by Bin Rank"
local label "s-intermediate-bin"
local notes \item Notes: Standard errors,  clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects and the baseline value of the outcome variable. ///
	Additional controls include zone fixed effects, age, ethnic group, household size, ///
 	and a household asset index. Parental and teacher effort indices are constructed by the way explained in ///
 	Table \ref{tab:balance}. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 
	
*** paper
head_mid, caption(`caption') label(`label') columns(10) size(scriptsize) sideways

esttab using "$tex_path/s_intermediate_binrank.tex", ///
	replace keep(merit relative m_b1sub15 r_b1sub15  b1_sub15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Attendance" " Study Hours" " Motivation to study hard" " Self esteem" " Grit" " Conscien tiousness" " Non-cognitive skill index" " Teacher effort index" " Parental effort" " Parents mentioned scholarship", pattern(1 1 1 1 1 1 1 1 1 1) /// 
 	span prefix(\multicolumn{@span}{p{0.54in}}{\centering) suffix(}) ///
			erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex} &\multicolumn{9}{c}{Sample: Grade 5-8}&\tabularnewline \cmidrule(r){2-11} \rule{0pt}{2ex}&\multicolumn{2}{c}{Student input}&\multicolumn{5}{c}{Non-cognitive skills}&\multicolumn{3}{c}{Teacher and parental response}\tabularnewline \cmidrule(r){2-3}\cmidrule(r){4-8}\cmidrule(r){9-11}\rule{0pt}{3ex}")) ///
	prehead("$header1") postfoot("$footer2")
est clear

** Long term results
** varaible grouping
	local t56i atd_abs_aa0m f1_studyhour_ac f1_motivation f1_s7_selfesteem f1_s7_conscientious s1_studyhour_ac s1_motivation s1_selfesteem s1_conscientious
	local cons56i atd_abs_bam b1_studyhour b1_motivation b1_s9_selfesteem b1_s9_conscientious b1_studyhour b1_motivation b1_s9_selfesteem b1_s9_conscientious

preserve
keep if y1_sub==1
keep if y1_standard==5|y1_standard==6
	forval k=1/9 {
	local i : word `k' of `t56i'
    local j : word `k' of `cons56i'
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)

	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	}

restore
local caption "Longer Term Intermediate Outcomes"
local label "s-intermediate-l"
local notes \item Notes: Standard errors,  clustered at the school-grade level, are in parentheses. ///
	All specifications include grade fixed effects, the baseline value of the outcome varlabie, zone fixed effects, ///
	age, ethnic group, household size, and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.  


*** paper
head_mid, caption(`caption') label(`label') columns(10) size(scriptsize) sideways

esttab est1 est3 est5 est7 est9 est11 est13 est15 est17 using "$tex_path/s_intermediate_long.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Attendance" " Study Hours" " Motivation to study hard" " Self esteem" " Conscien tiousness" " Study Hours" " Motivation to study hard" " Self esteem" " Conscien tiousness", pattern(1 1 1 1 1 1 1 1 1) /// 
 	span prefix(\multicolumn{@span}{p{0.4in}}{\centering) suffix(}) ///
			erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex} &\multicolumn{9}{c}{Sample: Grade 5-6}&\tabularnewline \cmidrule(r){2-10} \rule{0pt}{2ex}&\multicolumn{5}{c}{1st Follow-up}&\multicolumn{4}{c}{2nd Follow-up}\tabularnewline \cmidrule(r){2-6}\cmidrule(r){7-10} \rule{0pt}{2ex}&\multicolumn{2}{c}{Student input}&\multicolumn{3}{c}{Non-cognitive skills}&\multicolumn{1}{c}{Student input}&\multicolumn{3}{c}{Non-cognitive traits}\tabularnewline \cmidrule(r){2-3}\cmidrule(r){4-6}\cmidrule(r){7-7}\cmidrule(r){8-10}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")


mid_foot, notes(`notes') size(scriptsize) sideways

esttab est2 est4 est6 est8 est10 est12 est14 est16 est18 using "$tex_path/s_intermediate_long.tex", ///
	append keep(merit relative m_b1top15 r_b1top15  b1_top15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	prehead("$header2") posthead("$mid2")  postfoot("$footer2") 



*** presenation
head_foot, size(scriptsize) columns(6)

esttab est13 est14 est15 est16 est17 est18 using "$present_path/s_noncog_long_present.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(merit relative m_b1top15 r_b1top15 b1_top15) label ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Motivation to study hard" " Self esteem" " Conscientiousness" , pattern(1 0 1 0 1 0) /// 
		prefix(\multicolumn{@span}{p{0.8in}}{\centering ) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{6}{c}{Grade 5-6, 2nd f/u}\tabularnewline \cmidrule(r){2-7}\rule{0pt}{3ex}")) ///
	mlabels(none) collabels(none) nonotes addnotes() /// 
	substitute("\begin{threeparttable}" "\begin{adjustbox}{width=0.99\columnwidth} \begin{threeparttable}" ///
	"\end{threeparttable}" "\end{threeparttable} \end{adjustbox}") ///
	prehead("$header") postfoot("$footer")

estimates clear

****************************************************
/* 	 	Intermediate outomes: Presentatation tables: 				 */
****************************************************
*** Student investment
** Grade 5-8
preserve
keep if y1_sub==1
	forval k=1/2 {
	local i : word `k' of `t5'
    local j : word `k' of `cons5'
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster' 
	 estadd ysumm
	}
restore

** Grade 5-6
preserve
keep if y1_sub==1
keep if y1_standard==5|y1_standard==6
	forval k=1/3 {
	local i : word `k' of atd_abs_aa0m f1_studyhour_ac s1_studyhour_ac
    local j : word `k' of atd_abs_bam b1_studyhour b1_studyhour
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster' 
	 estadd ysumm
	}

restore
local caption "Students' Effort"
local label "s-input"
local notes \item Notes: Robust standard errors in parentheses. Standard errors clustered at the school-grade level. ///
	All specifications include grade fixed effects, zone fixed effects, baseline value of dependent variables, and demographic controls ///
	such as age, ethnic group, household size, and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.  

*** presentation
head_foot, size(footnotesize) columns(4)

esttab est1 est2 est4 est5 using "$present_path/s_input_present.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(merit relative m_b1top15 r_b1top15 b1_top15)  label ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups("Attendance" "Study Hours", pattern(1 0 1 0 1 0) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	mlabels(none) collabels(none) nonotes addnotes() /// 
	prehead("$header") postfoot("$footer")
	
estimates clear	

*** Motivation and Non-cognitive skills
preserve
keep if y1_sub==1
	forval k=1/5 {
	local i : word `k' of `t6' f1_noncog_index
    local j : word `k' of `cons6' b1_noncog_index
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster' 
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster' 
	 estadd ysumm
	}
restore

** Grade 5-6
preserve
keep if y1_sub==1
keep if y1_standard==5|y1_standard==6
	forval k=1/6 {
	local i : word `k' of f1_motivation f1_s7_selfesteem f1_s7_conscientious s1_motivation s1_selfesteem s1_conscientious 
    local j : word `k' of b1_motivation b1_s9_selfesteem b1_s9_conscientious b1_motivation b1_s9_selfesteem b1_s9_conscientious

	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster' 
	 estadd ysumm
	}
restore

local caption "Students Motivation and Non-Cognitive Traits"
local label "s-noncog"
local notes \item Notes: Robust standard errors in parentheses. Standard errors clustered at the school-grade level. ///
	All specifications include grade fixed effects, zone fixed effects, baseline value of dependent variables, and demographic controls ///
	such as age, ethnic group, household size, and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.  

*** presenation
head_foot, size(scriptsize) columns(10)

esttab est1 est2 est4 est5 est7 est8 est10 est11 est13 est14 using "$present_path/s_noncog_present.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(merit relative m_b1top15 r_b1top15 b1_top15) label ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Motivation to study hard" " Self esteem" " Conscientiousness" " Grit" " Non-cognitive skill index", pattern(1 0 1 0 1 0 1 0 1 0) /// 
		prefix(\multicolumn{@span}{p{0.8in}}{\centering ) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	mlabels(none) collabels(none) nonotes addnotes() /// 
	substitute("\begin{threeparttable}" "\begin{adjustbox}{width=0.99\columnwidth} \begin{threeparttable}" ///
	"\end{threeparttable}" "\end{threeparttable} \end{adjustbox}") ///
	prehead("$header") postfoot("$footer")

estimates clear


*** Teachers and family response
** Grade 5-8
preserve
keep if y1_sub==1
	forval k=1/3{
	local i : word `k' of `t7'
    local j : word `k' of `cons7'

	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster' 
	 estadd ysumm
	}
restore

** Grade 5-6
preserve
keep if y1_sub==1
keep if y1_standard==5|y1_standard==6
	forval k=1/5 {
	local i : word `k' of f1_teacherindex f1_parentaleffort_index f1_q503 s1_teacherindex s1_parentaleffort
    local j : word `k' of b1_teacherindex  b1_parentaleffort_index "" b1_teacherindex  b1_parentaleffort_index
	 eststo: reg `i' `s_spec' `j', `secluster'
	 estadd ysumm
  	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)
	 eststo: reg `i' `s_spec_top' `j', `secluster'
	 estadd ysumm
	 eststo: reg `i' `s_spec_sub' `j', `secluster' 
	 estadd ysumm

	}
restore

local caption "Teachers and Parental Response"
local label "s-teacher"
local notes \item Notes: Robust standard errors in parentheses. Standard errors clustered at the school-grade level. ///
	All specifications include grade fixed effects, zone fixed effects, and demographic controls such as age, ethnic group, household size, ///
	and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.


*** presentation
head_foot, size(scriptsize) columns(6)

esttab est1 est2 est4 est5 est7 est8  using "$present_path/s_teacher_present.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(merit relative m_b1top15 r_b1top15 b1_top15) label ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups("Teacher effort" "Parental effort" "Parents mention prgm", pattern(1 0 1 0 1 0) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	mlabels(none) collabels(none) nonotes addnotes() /// 
	prehead("$header") postfoot("$footer")
		
estimates clear

****************************************************
/* 		1.10  Appendix 1: Sample attrition				 */
****************************************************
** Grade 5-8
preserve
keep if y1_sub==1
	foreach v of varlist m1_e_p y1_fuq f1_e_p {
	 eststo: reg `v' merit relative i.y1_standard, `secluster'
	quietly estadd ysumm
	}

	foreach v of varlist y1_fuq f1_e_p {
	quietly eststo: reg `v' feedback i.y1_standard, `secluster'
	quietly estadd ysumm
	quietly eststo: reg `v' feedback merit relative i.y1_standard, `secluster'
	quietly estadd ysumm
	} 
restore

** Grade 5-6
preserve
keep if y1_sub==1
keep if y1_standard==5|y1_standard==6
	foreach v of varlist m1_e_p y1_fuq f1_e_p s1_p stu_tot_p_16_1 stu_tot_p_16_2 {
	quietly eststo: reg `v' merit relative i.y1_standard, `secluster'
	quietly estadd ysumm
	}

	foreach v of varlist y1_fuq f1_e_p s1_p stu_tot_p_16_1 stu_tot_p_16_2 {
	quietly eststo: reg `v' feedback i.y1_standard, `secluster'
	quietly estadd ysumm
	quietly eststo: reg `v' feedback merit relative i.y1_standard, `secluster'
	quietly estadd ysumm 
	} 
restore

local caption Sample Attrition
local label attrition
local notes \item Notes: Standard errors, in parentheses, are clustered at the school-grade level. ///
	All specifications include grade fixed effects. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 

*** Paper (Table A1)
*Manually insert a section header here: this is the only way I can figure out to have
*it on the same page as the table (will be rotated)

*Switching off for now
*global header1=subinstr("$header1","[htbp]","\section*{Appendix Tables}",.)

head_foot, caption(`caption') label(`label') columns(6) notes(`notes') 

	esttab est2 est3 est9 est10 est11 est12 using "$tex_path/attrition.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(merit relative) label  ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Survey" " Exam" " Survey" " Exam"  " Survey" " Exam" " Exam" , pattern(1 1 1 1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.5in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{6}{c}{Dependent Variable: Participated} \tabularnewline \cmidrule(r){2-7} " ///
		 "\rule{0pt}{2ex}&\multicolumn{2}{c}{Sample: Grade 5-8}&\multicolumn{4}{c}{Sample: Grade 5-6}\tabularnewline \cmidrule(r){2-3} \cmidrule(l){4-7}" ///
		 "\rule{0pt}{2ex}&\multicolumn{2}{c}{1st Follow-up}&\multicolumn{2}{c}{1st Follow-up}&\multicolumn{2}{c}{2nd Follow-up}\tabularnewline \cmidrule(r){2-3} \cmidrule(r){4-5}\cmidrule(r){6-7}\rule{0pt}{3ex}")) ///
		mlabels(none) collabels(none) nonotes addnotes() /// 	
	prehead("$header") postfoot("$footer")


local caption Sample Attrition (Feedback Treatment)
local label attrition-feedback
local notes \item Notes: Standard errors, in parentheses, are clustered at the school-grade level. ///
	All specifications include grade fixed effects. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 

*** Paper (Table B1)
head_foot,caption(`caption') label(`label') columns(6) notes(`notes') 

	esttab est4 est6 est14 est16 est18 est20 using "$tex_path/attrition_feedback.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(feedback) label ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Exam" " Survey" " Exam" " Exam" " Survey" " Exam" , pattern(1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{6}{c}{Dependent Variable: Participated}" ///
			" \tabularnewline \cmidrule(r){2-7}  \rule{0pt}{2ex} " ///
			"&\multicolumn{2}{c}{Sample: Grade 5-8}&\multicolumn{4}{c}{Sample: Grade 5-6}\tabularnewline " ///
			"\cmidrule(r){2-3} \cmidrule(l){4-7} \rule{0pt}{2ex} " ///
			" &\multicolumn{2}{c}{1st Follow-up}&\multicolumn{2}{c}{1st Follow-up}&\multicolumn{2}{c}{2nd Follow-up}\tabularnewline\cmidrule(r){2-3}\cmidrule(l){4-5}\cmidrule(l){6-7}  \rule{0pt}{3ex}")) ///
		mlabels(none) collabels(none) nonotes addnotes() /// 	
	substitute(	"\begin{threeparttable}" "\begin{minipage}{1.1\textwidth} \begin{center} \begin{threeparttable}" ///
	"\end{threeparttable}" "\end{threeparttable} \end{center} \end{minipage}") ///
	prehead("$header") postfoot("$footer")

*** Presentation
head_mid, label(`label') columns(8) size(footnotesize)

	esttab est1 est2 est3 est8 est9 est10 est11 est12 using "$present_path/attrition_present.tex", ///
	replace cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(merit relative) label  ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Exam" " Survey" " Exam" " Exam" " Survey" " Exam"  " Survey" " Exam" " Exam" , pattern(1 1 1 1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.5in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{3}{c}{Sample: Grade 5-8}&\multicolumn{5}{c}{Sample: Grade 5-6}\tabularnewline \cmidrule(r){2-4} \cmidrule(l){5-9} \rule{0pt}{2ex}&\multicolumn{1}{c}{Mid-term}&\multicolumn{2}{c}{1st Follow-up}&\multicolumn{1}{c}{Mid-term}&\multicolumn{2}{c}{1st Follow-up}&\multicolumn{2}{c}{2nd Follow-up}\tabularnewline \cmidrule(r){2-2} \cmidrule(r){3-4}\cmidrule(r){5-5} \cmidrule(l){6-7}\cmidrule(l){8-9}  \rule{0pt}{3ex}")) ///
		mlabels(none) collabels(none) nonotes addnotes() /// 
	substitute("\begin{threeparttable}" "\begin{adjustbox}{width=0.99\textwidth} \begin{threeparttable}") ///
	prehead("$header1") posthead("$mid1_2") postfoot("$footer1")


mid_foot, size(footnotesize)

	esttab est4 est6 est14 est16 est18 est20 using "$present_path/attrition_present.tex", ///
	append cells(b(fmt(3) star) se(par fmt(3)))  star(* 0.10 ** 0.05 *** 0.01) ///
	keep(feedback) label extracol(1 3) nonum ///
	varlabels(, end("" [1ex]) nolast) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mlabels(none) collabels(none) nonotes addnotes() nonumbers /// 
	substitute("{sidewaystable}" "{sideways}" "\end{threeparttable}" "\end{threeparttable} \end{adjustbox}") ///
	prehead("$header2") posthead("$mid2_2") postfoot("$footer2")

estimates clear

****************************************************
/* 		1.11  Appendix 2: Scholarship effect: Test score impact for each subject				 */
****************************************************
local fsub f1_z_chi f1_z_math f1_z_eng f1_z_prsc f1_z_ss f1_z_art f1_s8_score1
local bsub b1_r_chi b1_z_math b1_r_eng b1_r_prsc b1_r_ss b1_r_art b1_s10_score1

*** labels and table notes
local caption "Test Score Impacts for Each Subject"
local label "s-exam-subject"
local notes \item Notes: Standard errors,  clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects and the baseline value of the outcome variable, ///
	zone fixed effects, age, ethnic group, household size, ///
 	and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 

*** estimation and table output
	forval i=1/6 {
		local v : word `i' of `fsub'
	    local u : word `i' of `bsub'

	quietly eststo: reg `v' `s_spec' `u' , r cluster(y1_school_class)
	quietly estadd ysumm
	quietly test merit=relative
	quietly estadd scalar p_diff=r(p)

	quietly eststo: reg `v' `s_spec_top' `u' , r cluster(y1_school_class)
	quietly estadd ysumm

	quietly eststo: reg `v' `s_spec_sub' `u' , r cluster(y1_school_class)
	quietly estadd ysumm

}

*** paper
head_mid, caption(`caption') label(`label') columns(6) size(footnotesize)

	esttab est1 est4 est7 est10 est13 est16 using "$tex_path/s_exam_subject.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Chichewa" " Math" " English" " Science" " Social studies" " Art and Life skills" , pattern(1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.7in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{6}{c}{Dep. Var: Test score}\tabularnewline \cmidrule(r){2-7}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")

mid_foot, notes(`notes') size(footnotesize) columns(6) 

	esttab est2 est5 est8 est11 est14 est17 using "$tex_path/s_exam_subject.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(r2, fmt(3) labels("R-Squared"))  ///
	prehead("$header2") posthead("$mid2") postfoot("\hline") 

mid_foot, notes(`notes') size(footnotesize) columns(6) 

	esttab est3 est6 est9 est12 est15 est18 using "$tex_path/s_exam_subject.tex", ///
	append keep(merit relative m_b1sub15 r_b1sub15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	prehead("$header2") posthead("$mid3")  postfoot("$footer2") 


*** presentation
head_foot, size(scriptsize) columns(6)

esttab est1 est4 est7 est10 est13 est16 using "$present_path/s_exam_subject_present.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(N r2 ymean p_diff, fmt(0 3 3 3) labels("N" "R-Squared" "Mean of Dep. Var." "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Chichewa" " Math" " English" " Science" " Social studies" " Art and Life skills" , pattern(1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.5in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	substitute("\begin{tabular}" "\resizebox{\textwidth}{!}{\begin{tabular}" "\end{tabular}" "\end{tabular}}") ///
	prehead("$header") postfoot("$footer")
		
head_foot, size(scriptsize) columns(6)

esttab est2 est5 est8 est11 est14 est17 using "$present_path/s_exam_subject_present2.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15) `regtable' ///
	stats(N r2 ymean p_diff, fmt(0 3 3 3) labels("N" "R-Squared" "Mean of Dep. Var." "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Chichewa" " Math" " English" " Science" " Social studies" " Art and Life skills" , pattern(1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.5in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	substitute("\begin{tabular}" "\resizebox{\textwidth}{!}{\begin{tabular}" "\end{tabular}" "\end{tabular}}") ///
	prehead("$header") postfoot("$footer")
		
	est clear

/*-------------------------------------------------
		Appendix:  Final exam score by gender
-------------------------------------------------*/
*** generate gender dummy
	gen gen_1 = 1-male 
	gen gen_2 = male 

*** Control variables
	qui tab age, gen(age_)
	drop age_1
	loc control age_* b1_q105_chewa b1_hhsize b1_s5_asset

*** Create interaction terms using gender dummies
	foreach v of varlist merit relative b1_top15 m_b1top15 r_b1top15 b1_pct b1_z_total {
		gen gen_1_`v' = gen_1 * `v'
		gen gen_2_`v' = gen_2 * `v'
		lab var gen_1_`v' "`: variable label `v''"
		lab var gen_2_`v' "`: variable label `v''"
	}

	foreach v of varlist `control' {
		gen gen_1_`v' = gen_1 * `v'
		gen gen_2_`v' = gen_2 * `v'
		loc control_gen `control_gen' gen_1_`v' gen_2_`v'
		loc control_fem `control_gen' gen_1_`v'
	}
		loc control_gen `control_gen' gen_1 gen_2
		loc control_fem `control_fem' gen_1

*** Estimation
 ** Estimate Girl's equation
forval i = 1/2 {
	loc v: word `i' of f1_pct f1_z_total 
	loc u: word `i' of "gen_1_b1_pct gen_2_b1_pct" "gen_1_b1_z_total gen_2_b1_z_total"

		eststo: reg `v' gen_*_merit gen_*_relative `control_gen' `u', nocon r cluster(y1_school_class)
		eststo: reg `v' gen_*_merit gen_*_relative gen_*top15 `control_gen' `u', nocon r cluster(y1_school_class)
		qui sum `v' if gen_1==1&control==1
		estadd scalar ymeanc=r(mean)
}

 ** Swap the dummies (Girls <=> Boys)
	ren (gen_1*) (gen_3*)
	ren (gen_2*) (gen_1*)
	ren (gen_3*) (gen_2*)

 ** Estimate Boy's Euqation
forval i = 1/2 {
	loc v: word `i' of f1_pct f1_z_total 
	loc u: word `i' of "gen_1_b1_pct gen_2_b1_pct" "gen_1_b1_z_total gen_2_b1_z_total"

		eststo: reg `v' gen_*_merit gen_*_relative `control_gen' `u', nocon r cluster(y1_school_class)
		eststo: reg `v' gen_*_merit gen_*_relative gen_*top15 `control_gen' `u', nocon r cluster(y1_school_class)
		qui sum `v' if gen_1==1&control==1
		estadd scalar ymeanc=r(mean)
}

 ** Swap the dummies again
	ren (gen_1*) (gen_3*)
	ren (gen_2*) (gen_1*)
	ren (gen_3*) (gen_2*)

 ** Esimtate the Difference equation
forval i = 1/2 {
	loc v: word `i' of f1_pct f1_z_total 
	loc u: word `i' of "gen_1_b1_pct b1_pct" "gen_1_b1_z_total b1_z_total"

		eststo: reg `v' gen_1_merit gen_1_relative merit relative `control_fem' `control' `u', nocon r cluster(y1_school_class)
		eststo: reg `v' gen_1_merit gen_1_relative gen_1*top15 merit relative b1_top15 m_b1top15 r_b1top15 `control_fem' `control' `u', nocon r cluster(y1_school_class)
		qui sum `v' if control==1
		estadd scalar ymeanc=r(mean)
}

*** Write out table
local caption "Test Score Impacts by Gender"
local label s-exam-gender
local notes \item Notes: Standard errors, clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects, the baseline value of the outcome variable, ///
 	zone fixed effects, age, ethnic group, household size, and ///
 	a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01.

local model_header mgroups(" Girls" " Boys" " Girls - Boys" " Girls" " Boys" " Girls - Boys" , ///
	pattern(1 1 1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{3}{c}{Exam Rank}&\multicolumn{3}{c}{Exam Score} " ///
		"\tabularnewline\cmidrule(r){2-4}\cmidrule(r){5-7}\rule{0pt}{3ex}")) ///


head_mid, caption(`caption') columns(6) label(`label') size(footnotesize)

esttab est1 est5 est9 est3 est7 est11 using "$tex_path/s_exam_gender.tex", replace  ///
	keep(gen_1_merit gen_1_relative) ///
	`regtable' `model_header' ///
	stats(r2, fmt(3) labels("R-Squared"))  ///	
	prehead("$header1") postfoot("$footer1") posthead("$mid1")

mid_foot, columns(6) notes(`notes') size(footnotesize)

esttab est2 est6 est10 est4 est8 est12 using "$tex_path/s_exam_gender.tex", append ///
	keep(gen_1_merit gen_1_relative gen_1_b1_top15 gen_1_m_b1top15 gen_1_r_b1top15) ///
	stats(N r2 ymeanc, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///	
	`regtable' nonumbers ///
	prehead("$header2") posthead("$mid2") postfoot("$footer2") 

est clear

****************************************************
/* 		1.12 	Appendix 4: Scholarship effect: Test score impact (Noncognitive skills controlled)		 */
****************************************************
local noncog f1_motivation `t6'
foreach var of varlist `cons6' `t6' {
	gen `var'_top15=`var'*b1_top15
	local noncog_top15 "`noncog_top15' `var'_top15"
	}

preserve
keep if y1_sub==1
	forval i=1/2 {
	local v: word `i' of `t4'
	local j: word `i' of `cons4'
 
	eststo: reg `v' `s_spec_nocon' `noncog' `cons6' `j', `secluster'
	quietly estadd ysumm
	quietly test merit=relative
	quietly estadd scalar p_diff=r(p)

 	eststo: reg `v' `s_spec' `noncog' `cons6' `j', `secluster'
	quietly estadd ysumm

 	eststo: reg `v' `s_spec_top_nocon' `noncog' `cons6' `noncog_top15' `j', `secluster'
 	quietly estadd ysumm 	
	quietly test merit=relative
	quietly estadd scalar p_diff=r(p)

 	eststo: reg `v' `s_spec_top' `noncog' `noncog_top15' `j', `secluster'
 	quietly estadd ysumm 	
	}

	forval i=2/2 {
	local v: word `i' of `t4'
	local j: word `i' of `cons4'

 	quietly eststo: reg `v' `s_spec' `j', `secluster'
	quietly estadd ysumm
	quietly test merit=relative
	quietly estadd scalar p_diff=r(p)

 	quietly eststo: reg `v' `s_spec_top' `j', `secluster'
 	quietly estadd ysumm 	
	}

local caption "Test Score Impacts (Non-Cognitive Skills Controlled)"
local label "app-exam-intermediate"
local notes \item Notes: Standard errors, in parentheses, are clustered at the school-grade level. ///
	All specifications include grade fixed effects. Additional controls include zone fixed effects, ///
	age, ethnic group, household size, and a household asset index. ///
	Noncognitive skills include motivation, self esteem, grit, and  ///
	conscientiousness. * denotes significance at 0.10; ** at 0.05; *** at 0.01. 

*** paper
head_mid, caption(`caption') label(`label') columns(4) size(small)

	esttab est1 est2 est5 est6  using "$tex_path/app_exam_intermediate.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Exam Rank" " Exam score", pattern(1 0 1 0) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{4}{c}{Sample: Grade 5-8}\tabularnewline \cmidrule(r){2-5}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")


mid_foot, notes(`notes') size(small)

	esttab est3 est4 est7 est8 using "$tex_path/app_exam_intermediate.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	indicate("Additional Controls=`demo1'") ///	
	prehead("$header2") posthead("$mid2")  postfoot("$footer2") 

*** presentation
head_foot, size(scriptsize) columns(4)

esttab est2 est4 est6 est8 using "$present_path/app_exam_intermediate_present.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Exam Rank" " Exam Score", pattern(1 0 1 0) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	prehead("$header") postfoot("$footer")
		
	est clear
	restore

/****************************************************
 	1.13  Appendix 5: Scholarship effect: Test score impact by grade
*****************************************************/
forval i = 1/3 {
	loc type : word `i' of "" "_top" "_sub"
	forval j= 1/2 {
		loc condition : word `j' of "inlist(y1_standard,5,6,7)" "!inlist(y1_standard,5,6,7)"

		eststo: reg f1_z_total `s_spec`type'_nocon' b1_z_total if `condition', r cluster(y1_school_class)
		quietly estadd ysumm
		quietly test merit=relative
		quietly estadd scalar p_diff=r(p)

		eststo: reg f1_z_total `s_spec`type'' b1_z_total if `condition', r cluster(y1_school_class)
		quietly estadd ysumm
		quietly test merit=relative
		quietly estadd scalar p_diff=r(p)

	}	
}

*** paper
local caption "Test Score Impacts by Grade"
local label "s-exam-grade"
local notes \item Notes: Standard errors, clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects and the baseline value of the outcome variable. ///
 	Additional controls include zone fixed effects, age, ethnic group, household size, ///
 	and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 

head_mid, caption(`caption') label(`label') columns(4) size(footnotesize)

	esttab est1 est2 est3 est4 using "$tex_path/s_exam_standards.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Grade 5-7" " Grade 8" , pattern(1 0 1 0 ) /// 
		span prefix(\multicolumn{@span}{p{1.2in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")

mid_foot, notes(`notes') size(footnotesize) columns(4) 

	esttab est5 est6 est7 est8 using "$tex_path/s_exam_standards.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(r2, fmt(3) labels("R-Squared"))  ///
	prehead("$header2") posthead("$mid2") postfoot("\hline") 


mid_foot, notes(`notes') size(footnotesize) columns(4) 

	esttab est9 est10 est11 est12 using "$tex_path/s_exam_standards.tex", ///
	append keep(merit relative m_b1sub15 r_b1sub15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	indicate("Additional controls=`demo1'") ///	
	prehead("$header2") posthead("$mid3")  postfoot("$footer2") 


*** presentation
head_foot, size(scriptsize) columns(4)

esttab est2 est6 est4 est8 using "$present_path/s_exam_standards_present.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Grade 5-7" " Grade 8", pattern(1 0 1 0) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{4}{c}{Dep. Var: Exam Score}\tabularnewline \cmidrule(r){2-5}\rule{0pt}{3ex}")) ///
	prehead("$header") postfoot("$footer")
		
	est clear

****************************************************
/* 		1.14 	Appendix 6: Scholarship effect: Classroom environment 				 */
****************************************************
 ** Variable generation
	forval i=1/5{
	replace s1_q70`i'=. if s1_q70`i'==77
	}
	egen class_compete_idx = rowmean(s1_q702 s1_q703 s1_q704 s1_q705) if s1_q701~=.|s1_q702~=.|s1_q703~=.|s1_q704~=.|s1_q705~=.
	egen class_compete_idx2 = rowmean(s1_q701 s1_q702 s1_q703 s1_q704 s1_q705) if s1_q701~=.|s1_q702~=.|s1_q703~=.|s1_q704~=.|s1_q705~=.
	genindex s1_q701 s1_q702 s1_q703 s1_q704 s1_q705, prefix(class_compete) label("Classroom competitiveness index")
	
 ** Sample selection
preserve
	keep if y1_sub==1

 ** Estimation
	foreach v of varlist s1_q701 s1_q702 s1_q703 s1_q704 s1_q705 class_compete_index {
	eststo: reg `v' `s_spec' `j', `secluster'
	quietly estadd ysumm
	quietly test merit=relative
	quietly estadd scalar p_diff=r(p)
	
	eststo: reg `v' `s_spec_top' `j', `secluster'
	quietly estadd ysumm
	}

local caption "Classroom Environment"
local label "app-classroom"
local notes \item Notes: Standard errors, clustered at the school-grade level, are in parentheses. ///
	 All specifications include grade fixed effects, zone fixed effects, age, ethnic group, household size, ///
	 and a household asset index. The classroom competitiveness index is generated by taking the ///
	 average of the standardized measures of the other outcomes in the table, where the mean and ///
	 standard deviation in the control group is used in the standardization. ///
	 The resulting index is then standardized relative to the control group ///
	 . * denotes significance at 0.10; ** at 0.05; *** at 0.01. 

*** paper
head_mid, caption(`caption') label(`label') columns(6) size(small)

	esttab est1 est3 est5 est7 est9 est11 using "$tex_path/app_class_environment.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Smart students help friends better" " Willingness to help friends" " Received help from friends" ///
	 " Provided help to friends" " Asked for help from friends" " Classroom competitiveness index", pattern(1 1 1 1 1 1) /// 
		span prefix(\multicolumn{@span}{p{0.7in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{6}{c}{Sample: Grade 5-8}\tabularnewline \cmidrule(r){2-7}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")


mid_foot, notes(`notes') size(small)

	esttab est2 est4 est6 est8 est10 est12 using "$tex_path/app_class_environment.tex", ///
	append keep(merit relative b1_top15 m_b1top15 r_b1top15) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	prehead("$header2") posthead("$mid2")  postfoot("$footer2") 


*** presentation
head_foot, size(scriptsize) columns(6)

esttab est1 est2 est3 est4 est5 est6 using "$present_path/app_class_environment_present1.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Smart students help friends better" " Willingness to help friends" " Received help from friends", pattern(1 0 1 0 1 0) /// 
		span prefix(\multicolumn{@span}{p{0.8in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	prehead("$header") postfoot("$footer")
		
head_foot, size(scriptsize) columns(6)

esttab est7 est8 est9 est10 est11 est12 using "$present_path/app_class_environment_present2.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Provided help to friends" " Asked for help to friends" " Classroom competitiveness index", pattern(1 0 1 0 1 0) /// 
		span prefix(\multicolumn{@span}{p{0.8in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}")) ///
	prehead("$header") postfoot("$footer")
		
est clear
restore



****************************************************
/* 		1.2 	Baseline balance check (Feedback Treatment)				 */
****************************************************
preserve
keep if y1_sub==1
local rows: word count `t2a' `cons4' `cons5' `cons6' `cons7'

estimates clear

eststo: estpost summarize `t2a' `cons4' `cons5' `cons6' `cons7'
qui esttab *, cells(mean)
mat B1=r(coefs)
qui esttab *, cells(sd)
mat SE1=r(coefs)
mat P1=J(`rows',1,.)

estimates clear

eststo: estpost summarize `t2a' `cons4' `cons5' `cons6' `cons7' if feedback==0
qui esttab *, cells(mean)
mat B2=r(coefs)
qui esttab *, cells(sd)
mat SE2=r(coefs)
mat P2=J(`rows',1,.)

estimates clear

foreach var of varlist `t2a' `cons4' `cons5' `cons6' `cons7' {
	reg `var' feedback i.y1_standard, cluster(y1_school_class)
	estimates store `var'
	}

esttab *, cells(b) nocons keep(feedback)
mat B3=r(coefs)',r(stats)'
esttab *, cells(se) nocons
mat SE3=r(coefs)',J(`rows',1,.)
esttab *, cells(p) nocons
mat P3=r(coefs)',J(`rows',1,.)

estimates clear

mat B=B2,B3
mat SE=SE2,SE3
mat P=P2,P3

file open balance using "$tex_path/feedback_balance.tex", write replace

file write balance ///
	"\begin{table}[htbp]" _n ///
	"\centering" _n ///
	"\begin{footnotesize}" _n ///
	"\begin{threeparttable}" _n ///
	"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\caption{Balance of Baseline Variables Across Feedback Treatment\label{tab:balance-feedback}}" _n ///
	"\begin{tabular}{l*{3}{c}}" _n ///
	"\hline" _n ///
	" & No Feedback & Feedback vs. & \\" _n  ///
	" & Mean &  c & N \\" _n ///
	"\cmidrule(lr){2-4}" _n ///
	"&(1)&(2)&(3)\\" _n ///
	"\hline" _n
	
	
local i 1
foreach var in `t2a' `cons4' `cons5' `cons6' `cons7' {
	local label: var label `var'
	foreach j of num 1/1 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		local se`j'="[`se`j'']"
		}
	foreach j of num 2/3 {
		local b`j'=B[`i',`j']
		autoformat `b`j''
		local b`j': di $format `b`j''
		local pvalue=P[`i',`j']
		if `pvalue'<0.01 local star ***
		else if `pvalue'<0.05 local star **
		else if `pvalue'<.1 local star *
		else local star
		if "`star'"!="" local sym "\sym{`star'}"
		else local sym
		local b`j' `b`j''`sym'
		local se`j'=SE[`i',`j']
		autoformat `se`j''
		local se`j': di $format `se`j''
		local se`j'=strtrim("`se`j''")
		if `se`j''!=. local se`j'="(`se`j'')"
		else local se`j' 
		}
	file write balance ///
	"`label'&`b1'&`b2'&`b3' \\" _n ///
	" &`se1'&`se2'&`se3'\\" _n 
	if `i'<`rows' file write balance "[1ex]"_n 
	local ++i
	}
	
file write balance ///
	"\hline" _n ///
	"\end{tabular}" _n ///
	"\begin{tablenotes}[flushleft]" _n ///
	"\item Notes: Column 1 reports means of baseline variables for " ///
	"subjects assigned to the no feedback group. " ///
	"Column 2 reports the mean difference between the feedback treatment and the control group. " ///
	"Standard deviations are in brackets, and standard errors, clustered at the school-grade level, are in parentheses. " ///
	"Refer to the table note of Table \ref{tab:balance} for definition of other variables." ///
	"* denotes significance at 0.10; ** at 0.05; and *** at 0.01." _n ///
	"\end{tablenotes}" _n ///
	"\end{threeparttable}" _n ///
	"\end{footnotesize}" _n ///
	"\end{table}" 

file close balance


est clear
restore

****************************************************
/* 		1.8 	Feedback effect: Test score impacts				 */
****************************************************
preserve
keep if y1_sub==1
foreach k of numlist 2 5 {
	local i : word `k' of `t4' f1_q213g
    local j : word `k' of `cons4' b1_q216g
	quietly eststo: reg `i' `f_spec' `j', `secluster'
	quietly estadd ysumm
	quietly eststo: reg `i' `f_spec' `j' if m1_top15==1, `secluster'
	quietly estadd ysumm
	quietly eststo: reg `i' `f_spec' `j' if m1_top15==0, `secluster'
	quietly estadd ysumm
	quietly eststo: reg `i' `f_spec_hetero' `j', `secluster'
	quietly estadd ysumm
	quietly eststo: reg `i' `f_spec_hetero' `j' if m1_top15==1, `secluster'
	quietly estadd ysumm
	quietly eststo: reg `i' `f_spec_hetero' `j' if m1_top15==0, `secluster'
	quietly estadd ysumm
	} 	
restore

local caption "Feedback Effect: Test Score Impacts"
local label "f-exam"
local notes \item Notes: Standard errors,  clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects and the baseline value of the outcome variable. ///
	Additional controls include zone fixed effects, age, ethnic group, household size, ///
 	and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 

head_mid, caption(`caption') label(`label') columns(3) 

	esttab est1 est2 est3  using "$tex_path/f_exam.tex", ///
	replace keep(feedback) `regtable' ///
	stats(r2, fmt(3) labels("R-Squared"))  ///
	mgroups(" All" " Mid-term Top 15\%" " Mid-term Bot 85\%", pattern(1 1 1) /// 
		span prefix(\multicolumn{@span}{p{0.7in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{3}{c}{Sample: Grade 5-7}\tabularnewline \cmidrule(r){2-4} \rule{0pt}{2ex}&\multicolumn{3}{c}{Final exam}\tabularnewline \cmidrule(r){2-4}\rule{0pt}{3ex}")) ///
	substitute("Panel A" "Panel A: Average treatment effects") ///
	prehead("$header1") posthead("$mid1_2") postfoot("$footer1")


mid_foot, notes(`notes')

	esttab est4 est5 est6 using "$tex_path/f_exam.tex", ///
	append keep(feedback merit relative meritfeed relativefeed) `regtable' nonum ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	substitute("Panel B" "Panel B: Interaction effects of scholarhip and feedback") ///
	prehead("$header2")posthead("$mid2_2")  postfoot("$footer2") 

	
*** presentation
head_foot, size(scriptsize) columns(6) 

	esttab est1 est4 est2 est5 est3 est6 using "$present_path/f_exam_present.tex", ///
	replace keep(feedback merit relative meritfeed relativefeed) `regtable' noobs ///
	mgroups("  All" "  Top 15\%"  "  Bottom 15\%" , pattern(1 0 1 0 1 0) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{6}{c}{Dependent Variable: Final exam score (normalized)} \tabularnewline \cmidrule(r){2-7}\rule{0pt}{3ex}")) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean Dep. Var."))  ///
	prehead("$header") postfoot("$footer")

est clear


****************************************************
/* 		1.19 Appendix 13: relationship between self-evaluations, expectations and exam results and feedback intensity  */
****************************************************
preserve
keep if y1_sub==1

** variable generation
	gen diff = m1_pct-b1_pct // Difference in percentage ranking in midterm exam from baseline exam
	gen feeddiff = feedback * diff // Interaction term using feedback and diff

** labeling variables
	lab var diff "Mid-Base"
	lab var feeddiff "Feedback * (Mid-Base)"

*** Estimation 
	eststo: reg f1_q213g feedback diff feeddiff b1_z_total i.y1_zone i.y1_standard `demo' if merit==1|relative==1, `secluster'
	estadd ysumm
	eststo: reg f1_expect_scholarship2 feedback diff feeddiff b1_z_total i.y1_zone i.y1_standard `demo' if merit==1|relative==1, `secluster'
	estadd ysumm
	eststo: reg f1_z_total feedback diff feeddiff b1_z_total i.y1_zone i.y1_standard `demo' if merit==1|relative==1, `secluster'
	estadd ysumm


local caption "Self-evaluation, Expectations and Exam Scores by the Intensity of Feedback"
local label "f-additional"
local notes \item Notes: The sample includes students in the \textit{Standard} and \textit{Relative} scholarship groups. ///
	Self-evaluation is the student’s perception of performance within the classroom. ///
	Responses are scaled of 1 to 5, ranging  from [very bad (0-20\%)] to [very good (81-100\%)]. ///
	Expectation is a dummy variable equal to one if a student answered [very likely] or [likely] to the following question: ///
	[Based on your current position, how much do you think you have a chance of receiving a gift?] ///
	Standard errors, clustered at the school-grade level, are in parentheses. ///
	All specifications include grade fixed effects, zone fixed effects, baseline value of dependent variables, ///
	and demographic controls such as age, ethnic group, household size, and a household asset index. ///
	Mid-Base is a difference of percentile ranks between ///
	the midterm and baseline exam. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 

head_foot, caption(`caption') label(`label') notes(`notes') columns(3) 

	esttab using "$tex_path/f_additional.tex", ///
	replace keep(feedback diff feeddiff b1_z_total) `regtable' ///
	order(merit relative wmerit wrelative m_conlow r_conlow p_con) ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups(" Self-evaluation" " Expectation" " Exam score" , pattern(1 1 1) /// 
		span prefix(\multicolumn{@span}{c}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{3}{c}{Sample: Grade 5-7}\tabularnewline \cmidrule(r){2-4} \rule{0pt}{2ex}\rule{0pt}{3ex}")) ///
	prehead("$header") postfoot("$footer")
	
restore
estimates clear


********************************************************************************************************
/* 		1.4 	Appendix - Scholarship effect: Test score impacts (No feedback group) */
********************************************************************************************************
** Grade 5-8
preserve
keep if y1_sub==1
forval n=1/2 {
	loc condition: word `n' of "" "if feedback==0"
	forval k=1/2 {
	local i : word `k' of f1_pct f1_z_total
    local j : word `k' of b1_pct b1_z_total

	 eststo: reg `i' `s_spec' `j' `condition', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff=r(p)

	 eststo: reg `i' `s_spec_top' `j'  `condition', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff2_1=r(p)
	 quietly test merit+m_b1top15=relative+r_b1top15
	 quietly estadd scalar p_diff2=r(p)

	 eststo: reg `i' `s_spec_sub' `j'  `condition', `secluster'
	 estadd ysumm
	 quietly test merit=relative
	 quietly estadd scalar p_diff3_1=r(p)
	 quietly test merit+m_b1sub15=relative+r_b1sub15
	 quietly estadd scalar p_diff3=r(p)
	}
}
restore

local caption "Test Score Impacts on No Feedback Group"
local label "s-exam-nofeed"
local notes \item Notes: Standard errors,  clustered at the school-grade level, are in parentheses. ///
 	All specifications include grade fixed effects, the baseline value of the outcome variable, ///
	zone fixed effects, age, ethnic group, household size, ///
 	and a household asset index. * denotes significance at 0.10; ** at 0.05; and *** at 0.01. 
	
*** paper
 * short term result
head_mid, caption(`caption') label(`label') columns(4) size(footnotesize)

	esttab est1 est7 est4 est10 using "$tex_path/s_exam_a.tex", ///
	replace keep(merit relative) `regtable' ///
	stats(r2 p_diff, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel}"))  ///
	mgroups(" Full Sample" " No Feedback" " Full Sample" " No Feedback"  , pattern(1 1 1 1 ) /// 
		span prefix(\multicolumn{@span}{p{0.8in}}{\centering) suffix(}) ///
		erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{2ex}&\multicolumn{4}{c}{Sample: Grade 5-8}\tabularnewline \cmidrule(r){2-5}" ///
		"\rule{0pt}{2ex}&\multicolumn{2}{c}{Exam Rank}&\multicolumn{2}{c}{Exam Score}\tabularnewline \cmidrule(r){2-3}\cmidrule(r){4-5}\rule{0pt}{3ex}")) ///
	prehead("$header1") posthead("$mid1") postfoot("$footer1")


mid_foot, notes(`notes') size(footnotesize)

	esttab est2 est8 est5 est11 using "$tex_path/s_exam_a.tex", ///
	append keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' nonum ///
	stats(r2 p_diff2_1 p_diff2, fmt(3 3) labels("R-Squared" "P-value: \textit{Std = Rel} at Bot. 85\%" "P-value: \textit{Std = Rel} at Top 15\%"))  ///
	prehead("$header2") posthead("$mid2") postfoot("\hline") 

mid_foot, notes(`notes') size(footnotesize)

	esttab est3 est9 est6 est12 using "$tex_path/s_exam_a.tex", ///
	append keep(merit relative m_b1sub15 r_b1sub15) `regtable' nonum ///
	stats(N r2 ymean p_diff3_1 p_diff3, fmt(0 3 3 3) labels("N" "R-Squared" "Mean of Dep. Var." "P-value: \textit{Std = Rel} at Bot. 85\%" "P-value: \textit{Std = Rel} at Top 15\%"))  ///
	indicate("Controls=`demo1'") ///	
	prehead("$header2") posthead("$mid3")  postfoot("$footer2") 


*** presentation
head_foot, label(`label') size(footnotesize) columns(6)

esttab est4 est5 est10 est11 using "$present_path/s_exam_a_present_short.tex", ///
	replace keep(merit relative m_b1top15 r_b1top15 b1_top15) `regtable' ///
	stats(N r2 ymean, fmt(0 3 3) labels("N" "R-Squared" "Mean of Dep. Var."))  ///
	mgroups("Full Sample" "No Feedback", pattern(1 0 1 0) /// 
		prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}) ///
		begin("\rule{0pt}{1ex}&\multicolumn{4}{c}{Grade 5-8, Exam score} \tabularnewline \cmidrule(r){2-5} \rule{0pt}{2ex}")) ///	
	prehead("$header") postfoot("$footer")
est clear


********************************************************************************************************
/* 		1.4 	Appendix - Lee bounds test */
********************************************************************************************************
include "$do_path/lee bounds working.do"
